<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    private $_id;
	private $info;
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		
		$userModel = new User();
		$user = $userModel->find('username = :u',array(':u'=>$this->username));
		
		
		if($user==null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if($user->password !== $this->password)
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		else{
		   
            Yii::app()->user->setState('rights',$user->rights);
			if($user->rights == '1')
			  $this->setState('index',Yii::app()->createUrl('stubasicinfo/index'));
			else if(Yii::app()->user->rights == '3')
			  $this->setState('index',Yii::app()->createUrl('basicinfo/index'));
		    else
			  $this->setState('index',Yii::app()->createUrl('basicinfo/checkstuinfo'));	
			$this->_id = $user->uId;
			  
			  $this->setState('isNew',$user->isNew);	
			  if($user->rights === '1'){
			    //$this->setStu();
			  }
			
			$this->errorCode = self::ERROR_NONE;
	   }
		return !$this->errorCode;
	}
	public function setStu(){
	    
	}
	public function getId(){
	    return $this->_id;
	}
}